package com.qiantu.phone.alipsuh;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lxj.xpopup.XPopup;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.manager.ActivityManager;
import com.qiantu.phone.ui.activity.MessageCenterActivity;
import com.qiantu.phone.ui.dialog.MessagePopupDialog;

import java.util.Map;

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
        Activity topActivity = ActivityManager.getInstance().getTopActivity();
        MessagePopupDialog messagePopupDialog = new MessagePopupDialog(topActivity);
        messagePopupDialog.setDialogClickListener(new MessagePopupDialog.DialogClickListener() {
            @Override
            public void onClick() {
                Activity activity = ActivityManager.getInstance().getTopActivity();
                Intent intent = new Intent(activity, MessageCenterActivity.class);
                activity.startActivity(intent);
            }
        });
        new XPopup.Builder(topActivity)
                .dismissOnBackPressed(false)
                .dismissOnClickOutside(false)
                .isViewMode(true)
                .enableDrag(false)
                .hasShadowBg(false)
                .isFullScreen(false)
                .asCustom(messagePopupDialog);
        messagePopupDialog.setMessageType("200");
        messagePopupDialog.setMessageTitle(title);
        messagePopupDialog.setMessageContent(summary);
        messagePopupDialog.show();
    }

    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }

    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
        //extraMap:{_ALIYUN_NOTIFICATION_ID_=867042, _ALIYUN_NOTIFICATION_MSG_ID_=5704486180709248, Data={"houseSerialNo":102}, MessageType=4}
        //设备分享:1,门禁呼叫:2,成员管理邀请:3,家庭消息:4,场景延时任务:10,联动任务执行:20,设备状态提醒:30,房屋过户:40,设R备异常:100,环境异常:110,系统通知:200
        String messageType = extraMap.get("MessageType");
        String dataStr = extraMap.get("Data");
        Gson gson = new Gson();
        JsonObject data = gson.fromJson(dataStr, JsonObject.class);
        AppApplication.getInstance().showPushDialog(messageType, data, title, summary);
    }

    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }
}